﻿@model Actya.Web.ViewModels.Admin.EditSitemapViewModel
@section head {
	<script type="text/javascript" src="@Cms.Content("Themes/Admin/Scripts/jquery.watermark.min.js")"></script>
}
@{
	ViewBag.Title = "New sitemap item";
}
<h2>@ViewBag.Title</h2>

@using (Html.BeginForm())
{
	@Html.AntiForgeryToken()
	<fieldset>
		<div>
			@Html.LabelFor(m => m.NewNodeType)
			@Html.DropDownListFor(m => m.NewNodeType, new SelectList(Enum.GetNames(typeof(SitemapNodeType))), "-- Select type --", new { @class = "te fe-middle" })
			@Html.ValidationMessageFor(m => m.NewNodeType)
		</div>
		<div>
			@Html.LabelFor(m => m.NewNodeTitle)
			@Html.TextBoxFor(m => m.NewNodeTitle, new { @class = "te fe-wide" })
			@Html.ValidationMessageFor(m => m.NewNodeTitle)
		</div>
		<div>
			@Html.LabelFor(m => m.NewNodeParentId)
			<select id="NewNodeParentId" name="NewNodeParentId", class="te fe-middle">
				<option value="">No parent</option>
			</select>
		</div>
		<div id="newnodepagesection">
			@Html.LabelFor(m => m.NewNodePageId)
			@Html.HiddenFor(m => m.NewNodePageId)
			<input type="text" class="te fe-wide" id="newnodepage" />

		</div>
		<div id="newnodeurlsection">
			@Html.LabelFor(m => m.NewNodeUrl)
			@Html.TextBoxFor(m => m.NewNodeUrl, new { @class = "te fe-wide" })
		</div>
		<div>
			@Html.CheckBoxFor(m => m.NewNodeShowInNavigation)
			<label for="NewNodeShowInNavigation" class="inline">Show in navigation</label>
		</div>
		<div>
			@Html.CheckBoxFor(m => m.NewNodeShowInSitemap)
			<label for="NewNodeShowInSitemap" class="inline">Show in sitemap</label>
		</div>
	</fieldset>
	<p>
		<input type="submit" value="Save" /> or @Html.ActionLink("Cancel", "Index")
	</p>
}

<script type="text/javascript">
	$(document).ready(function () {
		$('#NewNodeParentId').focus(function () {
			if ($('#NewNodeParentId').children('option').length == 1) {
				var url = '@Url.Action("GetFlattenedSitemapNodes", "Sitemap")';
				$.getJSON(url, function (data) {
					$.each(data, function (idx, node) {
						var indent = node.Level * 20;
						if (node.PageId == null || node.PageId != $('#Id').val()) {
							$('#NewNodeParentId')
								.append($("<option></option>")
								.attr("value", node.Id)
								.attr("data-url", node.Url)
								.attr("style", "padding-left:" + indent + "px;")
								.text(node.Title));
						}
					});
					$('#NewNodeParentId').val("@Model.NewNodeParentId");
				});
			}
		});

		$("#newnodepage").autocomplete({
			minLength: 1,
			source: "@Url.Action("PagesAutocomplete", "Pages")",
			select: function(event, ui) {
				$('#newnodepage').val(ui.item.Title);
				$('#NewNodePageId').val(ui.item.Id);
				$('#NewNodeUrl').val(ui.item.Url);
				return false;
			}
		})
		.watermark('Type to search page')
		.data("autocomplete")._renderItem = function (ul, item) {
			return $("<li></li>")
				.data("item.autocomplete", item)
				.append("<a><strong>" + item.Title + "</strong><br>" + item.Url + "</a>")
				.appendTo(ul);
		};

		$('#NewNodeType').change(function() {
			var theType = $(this).val();
			if (theType == "Page") {
				$('#newnodepagesection').show();
				$('#newnodeurlsection').show();
				$('#NewNodeUrl').attr('disabled', 'disabled');
			}
			else if (theType == "Link") {
				$('#newnodepagesection').hide();
				$('#newnodeurlsection').show();
				$('#NewNodeUrl').removeAttr('disabled');
			}
			else {
				$('#newnodepagesection').hide();
				$('#newnodeurlsection').hide();
			}
		});
	})
</script>